
import json
from ability.models import Course, Chapter, Experiment

login = 1
courseIn = 2 # 学生进入课程页面
courseVisible = 20
chapterIn = 3 # request chapter content
chapterScroll = 4 # scroll chapter content
chapterOut = 4 # change chapter
courseOut = 5 # leave course detail
videoCoursePlay = 6  # play video
videoCourseDrag = 7 # drag video
videoCoursePause = 8  # click pause 
videoCourseEnd = 9
experimentIn = 10 # start experiment
experimentKeyEvents = 11 # key board or mouse events
experimentReportIn = 12 # 
experimentReportKeyEvents = 13 # 
experimentVisible = 22
experimentOut = 23
examIn = 14
examDetect = 15
examActive = 15
examKeyEvents = 16
paperIn = 17 # check exam paper result
paperKeyEvents = 18
paperOut = 19

def get_events_of_types(type):
  if type == 'course':
    return [courseIn, courseVisible, courseOut, chapterIn, chapterOut, chapterScroll, videoCoursePlay, videoCourseDrag, videoCoursePause, videoCourseEnd]
  if type == 'experiment':
    return [experimentIn, experimentKeyEvents, experimentReportIn, experimentReportKeyEvents, experimentVisible]
  if type == 'exam':
    return [examIn, examDetect, examKeyEvents]
  if type == 'paper':
    return [paperIn, paperKeyEvents, paperOut]

def get_event_class(event):
  if event == login:
    return loginEvent()
  if event == courseIn:
    return courseInEvent()
  if event == courseVisible:
    return courseVisibleEvent()
  if event == chapterIn:
    return chapterInEvent()
  if event == chapterOut:
    return chapterOutEvent()
  if event == experimentIn:
    return experimentInEvent()
  if event == experimentOut:
    return experimentOutEvent()
  if event == experimentVisible:
    return experimentVisibleEvent()

def get_event_detail(event, data):
  eventShowObj = get_event_class(event=event)
  if eventShowObj is None:
    return '未定义操作'
  return eventShowObj.to_detail(data)

class eventShow(): 
  def get_course(self, id):
    return Course.objects.filter(id=id).first()
  
  def get_chapter(self, id):
    return Chapter.objects.filter(id=id).first()

  def get_experiment(self, id):
    return Experiment.objects.filter(id=id).first()

  def to_detail(self, data):
    return ''

class loginEvent(eventShow):
  def toDetail(self, data):
    return '登录'

class courseInEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    course = self.get_course(datas['id'])
    return '浏览课程：' + course.course_name

class courseVisibleEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    course = self.get_course(datas['id'])
    action = '隐藏课程页面：' if datas['action'] == 'hidden' else '展示课程页面：'
    return action + course.course_name

class chapterInEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    chapter = self.get_chapter(datas['chapterId'])
    return '浏览章节：' + chapter.chapter_name


class chapterOutEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    chapter = self.get_chapter(datas['chapterId'])
    return '关闭章节：' + chapter.chapter_name

class experimentInEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    experiment = self.get_experiment(datas['id'])
    return '进入实验：' + experiment.experiment_name

class experimentOutEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    experiment = self.get_experiment(datas['id'])
    return '离开实验：' + experiment.experiment_name

class experimentVisibleEvent(eventShow):
  def to_detail(self, data):
    datas = json.loads(data)
    experiment = self.get_experiment(datas['id'])
    action = '隐藏实验页面：' if datas['action'] == 'hidden' else '展示实验页面：'
    return action + experiment.experiment_name